﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace JoomlaXMLEditor
{
    // zarządzanie plikami szablonu
    public partial class TemplateFiles : Form
    {
        // uchwyt do okna głównego
        public MainWindow parent;
        // lista węzłów plików
        public List<XmlNode> files;
        // konstruktor
        public TemplateFiles(MainWindow parent)
        {
            InitializeComponent();

            this.parent = parent;

            this.files = new List<XmlNode>();

            foreach (XmlNode node in this.parent.actualFile.GetElementsByTagName("files")[0].ChildNodes)
            {
                this.files.Add(node.CloneNode(true));

                if (node.HasChildNodes)
                {
                    this.listBox1.Items.Add(node.CloneNode(true).FirstChild.Value);
                }
            }
        }
        // zamykanie
        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // przesuwanie w górę
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > 0)
            {
                XmlNode tmpl1 = this.files[listBox1.SelectedIndex];
                this.files[listBox1.SelectedIndex] = this.files[listBox1.SelectedIndex - 1];
                this.files[listBox1.SelectedIndex - 1] = tmpl1;

                object tmpl2 = this.listBox1.Items[listBox1.SelectedIndex];
                this.listBox1.Items[listBox1.SelectedIndex] = this.listBox1.Items[listBox1.SelectedIndex - 1];
                this.listBox1.Items[listBox1.SelectedIndex - 1] = tmpl2;

                this.listBox1.SelectedIndex = listBox1.SelectedIndex - 1;
            }
        }
        // przesuwanie w dół
        private void button5_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                XmlNode tmpl1 = this.files[listBox1.SelectedIndex];
                this.files[listBox1.SelectedIndex] = this.files[listBox1.SelectedIndex + 1];
                this.files[listBox1.SelectedIndex + 1] = tmpl1;

                object tmpl2 = this.listBox1.Items[listBox1.SelectedIndex];
                this.listBox1.Items[listBox1.SelectedIndex] = this.listBox1.Items[listBox1.SelectedIndex + 1];
                this.listBox1.Items[listBox1.SelectedIndex + 1] = tmpl2;

                this.listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
        }
        // usuwanie
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                this.files.RemoveAt(listBox1.SelectedIndex);
                this.listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }
        // zapisywanie
        private void button6_Click(object sender, EventArgs e)
        {
            this.parent.actualFile.GetElementsByTagName("files")[0].RemoveAll();

            foreach (XmlNode node in this.files)
            {
                this.parent.actualFile.GetElementsByTagName("files")[0].AppendChild(node);
            }

            new TemplateXML(this.parent).refreshTree(this.parent.XMLTree, this.parent.actualFile);

            this.Close();
        }
        // dodawanie
        private void button1_Click(object sender, EventArgs e)
        {
            TemplateFileEditor editor = new TemplateFileEditor(this, -1);
            editor.Show();
        }
        // edycja
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex >= 0)
            {
                TemplateFileEditor editor = new TemplateFileEditor(this, this.listBox1.SelectedIndex);
                editor.Show();
            }
            else
            {
                MessageBox.Show("Proszę wybrać element do edycji");
            }
        }
    }
}
